package com.yst.sqlbuilder;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.Objects;

@Data
@Accessors(chain = true)
public class SQLLimit<T> implements ISQLExpression {

    private Integer maxRows;

    private Long offset;

    @Setter(AccessLevel.PRIVATE)
    SQLSelect<T> parentSelect;
    SQLLimit(SQLSelect<T> sqlSelect){
        this.parentSelect=sqlSelect;
    }

    public SQLSelect<T> getSelect(){
        return parentSelect;
    }

    @Override
    public String generateExpression(GenerateParam param) {
        String result="";
        if (Objects.nonNull(maxRows)) {
            result=result.concat("LIMIT "+maxRows);
            if (Objects.nonNull(offset)){
                result=result.concat(" ").concat("OFFSET "+offset);
            }
        }
        return result;
    }
}
